package com.azzaou.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

public class ExportUtil implements Serializable
{
	  
	 private static final long serialVersionUID = 1L;
	 private static final int DEFAULT_BUFFER_SIZE = 10240;
	
	 
	 public static void  prepareDownload(String filePath) throws IOException 
	  {
	      FacesContext context = FacesContext.getCurrentInstance();
	      HttpServletResponse response = (HttpServletResponse)context
	          .getExternalContext().getResponse();
	      File file = new File(filePath);
	      if (!file.exists())
	      {
	        response.sendError(HttpServletResponse.SC_NOT_FOUND);
	        return;
	      }
	      response.reset();
	      response.setBufferSize(DEFAULT_BUFFER_SIZE);
	      response.setContentType("application/vnd.ms-excel");
	      response.setHeader("Content-Length", String.valueOf(file.length()));
	      response.setHeader("Content-Disposition",
	          "attachment;filename=\"" + file.getName() + "\"");
	      BufferedInputStream input = null;
	      BufferedOutputStream output = null;
	      try
	      {
	        input = new BufferedInputStream(new FileInputStream(file),
	            DEFAULT_BUFFER_SIZE);
	        output = new BufferedOutputStream(response.getOutputStream(),
	            DEFAULT_BUFFER_SIZE);
	        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
	        int length;
	        while ((length = input.read(buffer)) > 0)
	        {
	          output.write(buffer, 0, length);
	        }
	      }
	      finally
	      {
	        input.close();
	        output.close();
	        if(file.exists())
	        {
	          file.delete();
	        }
	      }
	      context.responseComplete();
	  }
	 
	public static String getCellValue(String pValue) {
		String value = "";
		if (StringUtils.isNotBlank(pValue)) {
			value = pValue;
		}
		return value;
	}
}
